﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shop.Core.Data.Catalog;
using Shop.Core.Domain.Catalog;

namespace Shop.Data.IBatis.Catalog
{
    /// <summary>
    /// IBatis和MongoDB结合
    /// </summary>
    public class ProductRepository : DaoBase, IProductRepository
    {
        Shop.Data.Mongo.Catalog.ProductRepository mongoProduct = new Mongo.Catalog.ProductRepository();
        public Product GetById(string id)
        {
            return this.mongoProduct.GetById(id);
        }

        public Product GetByProductId(string productId)
        {
            return this.mongoProduct.GetByProductId(productId);
        }

        public bool Insert(Product product)
        {
            product.ProductId = this.GetProductId();
            product.Id = product.ProductId;
            return this.mongoProduct.Insert(product);
        }

        public bool Update(Product product)
        {
            return this.mongoProduct.Update(product);
        }

        public bool Delete(Product product)
        {
            return this.mongoProduct.Delete(product);
        }

        public IList<Product> FindByIdList(IList<string> idList)
        {
            throw new NotImplementedException();
        }

        protected virtual string GetProductId()
        {
            int i = this.Writer.QueryForObject<int>(this.GetSM("GetProductId"), null);
            return i.ToString("000000");
        }

        protected virtual string GetSM(string id)
        {
            return string.Format("Shop.Product.{0}", id);
        }
    }
}
